<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link href="../../static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" href="../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../static/js/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../static/vui/js/datagrid.js"></script>
	<script type="text/javascript" src="../../static/js/common.js"></script>
</head>
<body>
<div id="formDiv" style="width: 100%;height: 100%;">
	<div id="table"></div>
</div>
<script>
    $(function(){
        app.request({
            url:'account/select',
            success:function(resp){
                var accounts = resp.data;
                initAccountList(accounts);
            }
        });
    });

    function initAccountList(accountsData) {
        $('#table').datagrid({
            firstLoad:true,width:480,pagination:false,tableCss:{"margin":"0px"},
            columns:[
                {title:'操作',width:100,formatter:gridOpt},
                {field:'account',title:'账户',width:200,editor:'combobox',comboData:accountsData},
                {field:'money',title:'金额',width:180,editor:'decimal',n:'money'}
            ]
        });

        function gridOpt(row){
            var addSpan = $('<span class="fix-table-icon"><i class="fa fa-plus" style="color:green;"></i></span>');

            addSpan.click(function () {
                $('#table').datagrid("addRow",{xxid:app.DEFAULT_ALL_VALUE});
            });

            var delSpan = $('<span class="fix-table-icon"><i class="fa fa-remove" style="color:red;"></i></span></div>');
            delSpan.click(function () {
                var tr = $(this).parent().parent().parent();
                var len = tr.parent().find('.vui-table-row').length;
                if( len == 1 ){
                    app.alert(parent.layer,'最后一行不能删除',2);
                }else{
                    tr.remove();
                }
            });

            var outDiv = $('<div></div>');
            outDiv.append(addSpan).append(delSpan);;

            return outDiv;
        }

        var parAccounts = parent.accounts;
        if(parAccounts.length > 0){
            var rows = [];
            for( var i=0 ; i<parAccounts.length ; i++ ){
                var row = parAccounts[i];
                rows[i] = {account:row.accountName,money:row.amount};

            }
            $('#table').datagrid("addRows",rows);
            var divRows = $('#table .vui-table-row');
            for( var i=0 ; i<parAccounts.length ; i++ ){
                divRows.eq(i).find(' > .vui-table-cell[field="account"]').data({value:row.accountId,text:row.accountName});
            }

        }else{
            $('#table').datagrid("addRow",{});
        }

        app.getOkBtn().click(function(){
            var accountDatas = [];
            var totalAmount = '0.00';
            $('#table .vui-table-row').each(function () {
                var account = $(this).find(' > .vui-table-cell[field="account"]').data();
                var money = $(this).find(' > .vui-table-cell[field="money"]').html();

                if( account.value && money.length==0 ){
                    app.message(parent.layer,'账户['+account.text+']未填写金额',2);
                    accountDatas = [];
                    return false;
                }

                if( account.value && money.length>0 ){
                    money = app.mul(money,app.DIV);
                    totalAmount = app.add(money,totalAmount);
                    accountDatas[accountDatas.length] = {accountId:account.value,amount:money,accountName:account.text};
                }
            });

            if( accountDatas.length == 0 ){
                parent.$('#accountId').combobox('setValue',{text:'',value:''});
                $('input[name="order.realMoney"]',parent.document).removeAttr("readonly");
            }

            $('input[name="order.realMoney"]',parent.document).val(app.div(totalAmount,100));
            parent.autoCalc();
            parent.accounts = accountDatas;

            app.closeLastWindow();
        });
    }

</script>
</body>
</html>